package com.example.a2032.activity

import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.a2032.R
import com.example.a2032.Utils.Utils
import com.example.a2032.adapter.UnreadMsgAdapter
import com.example.a2032.viewmodel.UnreadMsgViewModel
import kotlinx.android.synthetic.main.activity_unread_msg.*
import java.util.*

class UnreadMsgActivity : BaseActivity() {
    private val mViewModel by viewModels<UnreadMsgViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_unread_msg)
        init()
    }

    fun init() {
        val iconArrays = Utils.getResourcesImageArray(this, R.array.item_chatImgId)
        val layoutManager = LinearLayoutManager(this)
        layoutManager.orientation = LinearLayoutManager.VERTICAL
        val array = resources.getStringArray(R.array.kinship_array);
        mViewModel.unReadList?.observe(this, androidx.lifecycle.Observer {
            if (it.size == 0) {
                finish()
            } else {
                val adapter = UnreadMsgAdapter(it, array, iconArrays);
                unReadMsgRecyclerView.adapter = adapter
            }

        })

        val dividerItemDecoration: DividerItemDecoration = DividerItemDecoration(this, DividerItemDecoration.VERTICAL)
        dividerItemDecoration.setDrawable(resources.getDrawable(R.drawable.bg_divider))

        unReadMsgRecyclerView.apply {
            this.layoutManager = layoutManager

        }
        unReadMsgRecyclerView.addItemDecoration(dividerItemDecoration)
        mViewModel.getUnReadList()
        // EMClient.getInstance().chatManager().markAllConversationsAsRead()
    }

    override fun onResume() {
        super.onResume()
        mViewModel.getUnReadList()
    }

    override fun onDestroy() {
        super.onDestroy()

    }
}